// pages/adddetail/adddetail.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        alllist:[],
        cpxq:[],
    },
    
    naveback() {
        setTimeout(() => {
            wx.navigateBack()
        }, 300);
    },

    adddata1(){
        let reqdata = {
            "type": "文字",
		    "content": "",
        }
        let newlist = this.data.cpxq
        newlist.push(reqdata)
        this.setData({
            cpxq: newlist
        })
    },
    adddata2(){
        let reqdata = {
            "type": "图片",
		    "content": [],
        }
        let newlist = this.data.cpxq
        newlist.push(reqdata)
        this.setData({
            cpxq: newlist
        })
    },
    adddata3(){
        let reqdata = {
            "type": "视频",
		    "content": [],
        }
        let newlist = this.data.cpxq
        newlist.push(reqdata)
        this.setData({
            cpxq: newlist
        })
    },


    delitem(items){
        let setdatas = this.data.cpxq
        setdatas.splice(items.currentTarget.dataset.select, 1)
        this.setData({
            cpxq: setdatas
        });
    },

    upjia(item) {
        if (item.currentTarget.dataset.lined == 0) {
            wx.showToast({
                title: '已经是第一个了',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        let aa = this.data.cpxq
        var temp = aa.splice(item.currentTarget.dataset.lined, 1);
        aa.splice(item.currentTarget.dataset.lined - 1, 0, temp[0]);
        this.setData({
            cpxq: aa
        })
    },
    dinwjia(item) {
        if (item.currentTarget.dataset.lined + 1 == this.data.cpxq.length) {
            wx.showToast({
                title: '已经是最后一个了',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        let aa = this.data.cpxq
        var temp = aa.splice(item.currentTarget.dataset.lined, 1);
        aa.splice(item.currentTarget.dataset.lined + 1, 0, temp[0]);
        this.setData({
            cpxq: aa
        })
    },

    textchange(items){
        console.log(items)
        let setdatas = `cpxq[${items.currentTarget.dataset.select}].content`
        this.setData({
            [setdatas]: items.detail
        });
    },

    imgafterRead(event) {
        let setdatas = `cpxq[${event.currentTarget.dataset.select}].content`
        const { file } = event.detail;
        const filebangList = this.data.cpxq[event.currentTarget.dataset.select].content;
        filebangList.push(...file);
        this.setData({
            [setdatas]: filebangList
        });
    },
    imgdel(event) {
        let setdatas = `cpxq[${event.currentTarget.dataset.select}].content`
        let filebangList = this.data.cpxq[event.currentTarget.dataset.select].content;;
        filebangList.splice(event.detail.index, 1)
        this.setData({
            [setdatas]: filebangList
        });
    },
    
    vidafterRead(event) {
        console.log(event)
        let setdatas = `cpxq[${event.currentTarget.dataset.select}].content`
        const { file } = event.detail;
        const filebangList = this.data.cpxq[event.currentTarget.dataset.select].content;
        console.log(this.data.cpxq[event.currentTarget.dataset.select].content)
        filebangList.push(...file);
        this.setData({
            [setdatas]: filebangList
        });
    },
    viddel(event) {
        let setdatas = `cpxq[${event.currentTarget.dataset.select}].content`
        let filebangList = this.data.cpxq[event.currentTarget.dataset.select].content;
        filebangList.splice(event.detail.index, 1)
        this.setData({
            [setdatas]: filebangList
        });
    },

    backsetdata() {
        let newcpxq = []
        for (const i in this.data.cpxq) {
            console.log("a")
            if (typeof(this.data.cpxq[i].content) == "string" &&  this.data.cpxq[i].content != "" || typeof(this.data.cpxq[i].content) == "object" &&  this.data.cpxq[i].content.length != 0) {
                newcpxq.push(this.data.cpxq[i])
            }
        }
        console.log(newcpxq)
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        prevPage.setData({
            "cpxq": newcpxq,
        })
        setTimeout(() => {
            this.naveback()
        }, 600);
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options)
        if (options.cpxq == '') {
            
        }else{
            this.setData({
                cpxq:JSON.parse(options.cpxq)
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})